#include <printf.h>
#include <math.h>

int asc(int x, int y) //若x>y，则算逆序，返回1
{
    if (x > y) return 1;
    else return 0;
}

int desc(int x, int y) {
    if (x > y) return 0;
    else return 1;
}

int ascAbs(int x, int y) {
    if (fabs(x) > fabs(y)) return 1;
    else return 0;
}


void sort(int a[], int n, int (*cmp)(int, int)) {
    int i, j;
    for (i = 0; i < n - 1; i++)
        for (j = i + 1; j < n; j++)
            if ((*cmp)(a[i], a[j])) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
}

int main() {
    int a[5] = {3, -4, 1, 5, 2};
    int select;
    int (*funp)(int, int);
    while (1) {
        printf("请选择排序方式：\n");
        printf("1.升序\n");
        printf("2.降序\n");
        printf("3.按绝对值升序\n");
        printf("4.退出\n");
        scanf("%d", &select);
        if (select == 4)break;

        switch (select) {
            case 1:
                funp = asc;
                break;
            case 2:
                funp = desc;
                break;
            case 3:
                funp = ascAbs;
        }
        sort(a, 5, funp);
        for (int i = 0; i < 5; i++)
            printf("%d ", a[i]);
        printf("\n");
    }
    printf("程序已终止!");
}




